$(document).ready(function() {
    var queryString = location.search;
    var parameters = Request.parseQueryString(queryString);

    var queryStartTime = null;
    var queryEndTime = null;

    if ( parameters.start && parameters.end ) {
        queryStartTime = new Date(Number(parameters.start));
        queryStartTime.setMinutes(queryStartTime.getMinutes() + 1);
        queryEndTime = new Date(Number(parameters.end));
    }

    var $queryStartTime = $('.input-start-time').datepicker({
    });
    $queryStartTime.datepicker('update', new Date());
    if ( queryStartTime ) {
        $queryStartTime.datepicker('update', queryStartTime);
    }

    var $queryEndTime = $('.input-end-time').datepicker({
    });
    $queryEndTime.datepicker('update', new Date());
    if ( queryEndTime ) {
        $queryEndTime.datepicker('update', queryEndTime);
    }

    var $inputStudentId = $('.input-sid');
    if ( parameters.sid ) {
        $inputStudentId.val(parameters.sid);
    }

    var $inputStudentName = $('.input-name');
    if ( parameters.name ) {
        $inputStudentName.val(decodeURI(parameters.name));
    }

    var $inputStatus = $('#teacher_search_status');
    var status = parameters.status;
    if ( 'status' in parameters ) {
        $inputStatus.val(status);
        var statusName = $('.menu-status a[value="' + status + '"]').text();
        $inputStatus.parent('a').find('span').text(statusName);
    }

    var $inputNatureClass = $('#teacher_search_class');
    var natureClass = parameters.nature_class;
    if ( natureClass ) {
        $inputNatureClass.val(natureClass);
        var natureClassName = $('.menu-class a[value="' + natureClass + '"]').text();
        $inputNatureClass.parent('a').find('span').text(natureClassName);
    }

    $('.btn-search').click(function(e) {
        disableAll(e);
    
        var startTime = $queryStartTime.datepicker('getDate');
        startTime = new Date(startTime.getFullYear(), startTime.getMonth(), startTime.getDate());
        var endTime = $queryEndTime.datepicker('getDate');
        endTime = new Date(endTime.getFullYear(), endTime.getMonth(), endTime.getDate());
    
        startTime.setMinutes(startTime.getMinutes() - 1);
        endTime.setDate(endTime.getDate() + 1);
        endTime.setMinutes(endTime.getMinutes() - 1);

        var sid = $inputStudentId.val();
        var name = $inputStudentName.val();
        var status = $inputStatus.val();
        var natureClass = $inputNatureClass.val();

        // 必填参数
        var queryCondition = {
            start: startTime.getTime(),
            end: endTime.getTime(),
            status: status
        };

        // 可选参数
        if ( sid ) {
            queryCondition.sid = sid;
        }
        if ( name ) {
            queryCondition.name = name;
        }
        if ( natureClass != '0' ) {
            queryCondition.nature_class = natureClass;
        }

        request = new Request('/teacher/attendance/search', queryCondition);

        response = new Response();
        response.redirectTo(request);
    });
});

function tableDetailButtonClicked(event) {
    var detailButton = event.currentTarget;
    if (detailButton.innerHTML == '返回') {
        $(detailButton).removeClass('table_detail_background');
        $(detailButton).removeClass('table_detail_button_back');
        detailButton.innerHTML = '详细 <i class="iconfont icon-arrow-down attend_table_detail_arrow"></i>';
        var detail_content_id = 'attend_detail_content' + detailButton.getAttribute('data-id');
        var detail_area_id = 'attend_detail_area' + detailButton.getAttribute('data-id');
        $("[data-id="+detail_content_id+"]").animate({
            'height': '0',
            'padding': '0',
            'line-height': '0'
        }, 'fast', function () {
            $("[data-id="+detail_area_id+"]").addClass('table_detail_content_hide');
        });
    }
    else {
        $(detailButton).addClass('table_detail_background');
        $(detailButton).addClass('table_detail_button_back');
        detailButton.innerHTML = '返回';
        var detail_content_id = 'attend_detail_content' + detailButton.getAttribute('data-id');
        var detail_area_id = 'attend_detail_area' + detailButton.getAttribute('data-id');
        $("[data-id="+detail_area_id+"]").removeClass('table_detail_content_hide');
        $("[data-id="+detail_content_id+"]").animate({
            'height': 'auto',
            'padding': '6px 8px',
            'line-height': '18px'
        }, 'fast');
    }
}
